package com.authine.cloudpivot.ext.controller;

import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.bizmodel.BizDataRuleModel;
import com.authine.cloudpivot.engine.api.model.bizmodel.BizSchemaModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectQueryModel;
import com.authine.cloudpivot.engine.component.query.api.FilterExpression;
import com.authine.cloudpivot.engine.component.query.api.Page;
import com.authine.cloudpivot.engine.component.query.api.helper.PageableImpl;
import com.authine.cloudpivot.engine.component.query.api.helper.Q;
import com.authine.cloudpivot.ext.utils.SchemaUtils;
import com.authine.cloudpivot.web.api.controller.runtime.AbstractFormRuntimeController;
import com.authine.cloudpivot.web.api.util.AuthUtils;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import com.authine.cloudpivot.web.api.view.runtime.FormDataVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @Author: brookpeng
 * @Date: 2022/3/24 0:08
 */
@Slf4j
@RestController
@RequestMapping("/api/public/pageUseRunTime")
public class PageUseRuntimeController extends AbstractFormRuntimeController {

    private  String adminId = "2c9280a26706a73a016706a93ccf002b";

    @GetMapping("/superLoad")
    public ResponseResult<FormDataVO> superLoad(@RequestParam String objectId,
                                                @RequestParam String schemaCode,
                                                @RequestParam String sheetCode) {
        FormDataVO formData = new FormDataVO();
        setFormDataBySheet(formData, adminId, objectId, schemaCode, sheetCode, null, null, null, null);
        assembleBizDataRuleModel(formData);
        isliked(formData);
        return getOkResponseResult(formData, "成功调用superLoad接口");
    }

    /**
     * 组装数据规则数据
     *
     * @param formData
     */
    private void assembleBizDataRuleModel(FormDataVO formData) {
        BizSchemaModel bizSchemaModel = formData.getBizSchema();
        if (Objects.isNull(bizSchemaModel)) {
            return;
        }

        List<BizDataRuleModel> bizDataRuleModels = getBizDataRuleFacade().getListBySchemaCode(bizSchemaModel.getCode());
        if (CollectionUtils.isEmpty(bizDataRuleModels)) {
            return;
        }

        bizDataRuleModels.sort(Comparator.comparing(BizDataRuleModel::getDataRuleType));
        bizSchemaModel.setBizDataRuleModels(bizDataRuleModels);
    }

    @PersistenceContext
    EntityManager entityManager;

    private void isliked(FormDataVO formData){

        try {
            formData.getBizObject().put("sfydz", false);
            String bizId = formData.getBizObject().getId();

            String sql = "select count(*) as count from ".concat(getBizObjectFacade().getTableName(SchemaUtils.liked)).concat("  where creater=:creater and courseId =:courseId limit 1");

            BigInteger count = (BigInteger) entityManager.createNativeQuery(sql).setParameter("creater", getUserId())
                    .setParameter("courseId", bizId).getSingleResult();

            if (count != null && count.intValue() > 0) {
                formData.getBizObject().put("sfydz", true);
            }
        } catch (Exception e) {
            log.error("获取是否点赞异常:".concat(e.getMessage()), e);
        }

    }
}
